package com.nstar.base.controls
{
	import com.nstar.base.util.ColorUtils;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.events.FlexEvent;
	
	/**
	 * 带默认提示的输入框
	 * 
	 * @author 李嘉陵
	 * @since 2007-05-31
	 * modified by zhucm , 2007.9.10
	 */
	public class HintTextInput extends TextInput
	{
		public var defultText:String = ""
		public var defultTextColor:String = "#AAAAAA"
		
		private var clearButton:Button;
		
		[Embed(source="../assets/WindowCloseButton3.gif")]
		public static const WINDOW_CLOSE_BUTTON_1:Class;
		[Embed(source="../assets/WindowCloseButton4.gif")]
		public static const WINDOW_CLOSE_BUTTON_2:Class;
		
		//是否必须，如果是必须属性，则在输入框前面加上一个红色的*号
		public var required:Boolean = false;
		var lblRequired:Label ;
		
		public function HintTextInput(){
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, setDefaultText);
			this.addEventListener("textChanged", showClearButton);
			this.addEventListener(FocusEvent.FOCUS_IN, this_focusInHandler)
			this.addEventListener(FocusEvent.FOCUS_OUT, this_focusOutHandler)
		}
		
		public function set value(v:String):void {
			this.text = v;
		}
		
		public function get value():String {
			return this.text;
		}
		
		private function setDefaultText(event:Event):void{
			
			if(clearButton == null){
				clearButton = new Button();
				clearButton.width=10;
				clearButton.height=10;
				clearButton.y = (this.height - 10) / 2;
				clearButton.x = this.width - 10 - (this.height - 10) / 2;
				clearButton.focusEnabled=false;
				 
				clearButton.setStyle("upSkin", WINDOW_CLOSE_BUTTON_1);
				clearButton.setStyle("overSkin", WINDOW_CLOSE_BUTTON_2);
				clearButton.setStyle("downSkin", WINDOW_CLOSE_BUTTON_2); 
				
				clearButton.addEventListener(MouseEvent.CLICK, clearButton_clickHandler);
				clearButton.visible = false;
				clearButton.buttonMode = true;
				clearButton.useHandCursor = true;
				clearButton.mouseChildren = false;
				this.addChild(clearButton);
				this.addEventListener("textChanged", showClearButton);
				showClearButton(null);
			}
			
			if(text == ""){
				this.text = "";
				this.textField.text = defultText;
				this.textField.textColor = ColorUtils.convertColor(defultTextColor);
			}
			//如果是必须属性，在输入框前面加上红色的*号
			if (required){
				lblRequired = new Label();
				lblRequired.text="*";
				lblRequired.setStyle("color",0xff0000);
				lblRequired.width=10;
				lblRequired.height =10;
				
				this.addChild(lblRequired); 				
			}
		}
		
		private function showClearButton(event:Event):void{
			if(clearButton){
				if(text != ""){
					clearButton.visible = true;
				}else{
					clearButton.visible = false;
				}
				this.textField.textColor = this.getStyle("color");
			}
		}
		
		private function clearButton_clickHandler(event:MouseEvent):void{
			if(this.enabled && this.editable){
				this.text = "";
				clearButton.visible = false;
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		private function this_focusInHandler(event:FocusEvent):void{
			if(text == ""){
				this.text = "";
				this.textField.text = "";
				this.textField.textColor = this.getStyle("color");
				clearButton.visible = false;
			}else if( text.length > 0 ) {
				clearButton.visible = true;
			}
			
		}
		
		private function this_focusOutHandler(event:FocusEvent):void{
			setDefaultText(event);
		}
		

				
		 
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			if(clearButton != null){
				clearButton.x = this.width - 10 - (this.height - 10) / 2;
				clearButton.y = (this.height - 10) / 2;
			}
			
			if (lblRequired!=null){
				lblRequired.x=-5;
				lblRequired.y= 5;;	
			}			
		}
	}
}